var baseurl='http://www.yikutian.com/wx/';
var YKT = {};
var ajax = {
    baseurl: 'http://www.yikutian.com/wx/ykthdservice.asmx/yktservice',
    timeout: 10000,
    request: function (config) {
        console.log('Params=====' + JSON.stringify(config.params))
        $.ajax({
            type: config.method == null ? 'post' : config.method,
            url: ajax.baseurl,
            data: config.params,
            dataType: "json",
            timeout: ajax.timeout,
            cache: false,
            success: function (data) {
                console.log('Ajax success===' + JSON.stringify(data));
                if (!data.ResultCode) {
                    config.callback(data.ResultData);
                } else {
                    alert(data.ResultMsg);
                }
            },
            error: function (xhr, type, errorThrown) {
                console.log(xhr.status);
                console.log(type);
                console.log(errorThrown);
            }
        });
    },
    returnRequest: function (config) {
        console.log('Params=====' + JSON.stringify(config.params))
        $.ajax({
            type: config.method == null ? 'post' : config.method,
            url: ajax.baseurl,
            data: config.params,
            dataType: "json",
            timeout: ajax.timeout,
            cache: false,
            success: function (data) {
                console.log('Ajax success===' + JSON.stringify(data));
                config.callback(data)
            },
            error: function (xhr, type, errorThrown) {
                console.log(xhr.status);
                console.log(type);
                console.log(errorThrown);
            }
        });
    },
}
// var data = {
//     "ResultData": {
//         "id": null,
//         "wxopenid": "o___NwxGgyGSDm5L4cPJ-W9Iyn9I",
//         "truename": "贾鹏",
//         "headurl": "http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLEI5YWbmCwmXuIsDhext80Umy68eJo2zmeqXYkyoUPc4O0etTHWZzz1MUcIKicA1pibQVG3pHtBpnw/132"
//     },
//     "ResultCode": 0,
//     "ResultMsg": "授权成功"
// };

function setUserInfo(data) {
    localStorage.setItem('userInfo', JSON.stringify(data));
}

function getUserInfo() {
    return JSON.parse(localStorage.getItem('userInfo'));
}

function clearUserInfo() {
    localStorage.clear();
}


// 获取url地址
function isUserInfo(fn) {
    if (getUserInfo().ResultData.id!='') {
        console.log(getUserInfo())
        fn()
    } else {
        ajax.request({
            params: {
                action: 'wxsetting'
            },
            callback: function (res) {
                console.log(decodeURI(YKT.QueryString("openid")));
                console.log(decodeURI(YKT.QueryString("NickName")));
                console.log(decodeURI(YKT.QueryString("HeadimgUrl")));
                if (decodeURI(YKT.QueryString("openid")) != '') {
                    // var data ="{ \"ResultData\": { \"wxopenid\":'" +decodeURI(YKT.QueryString("openid")) + "',\"truename\": '" + decodeURI(YKT.QueryString("NickName")) + "',\"headurl\": \"" +  + "\"}, \'ResultCode\': 0, \'ResultMsg\': \"请求成功\" }";
                    // var data = {
                    //     "ResultData": {
                    //         "id": null,
                    //         "wxopenid": "o___NwxGgyGSDm5L4cPJ-W9Iyn9I",
                    //         "truename": "贾鹏",
                    //         "headurl": "http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLEI5YWbmCwmXuIsDhext80Umy68eJo2zmeqXYkyoUPc4O0etTHWZzz1MUcIKicA1pibQVG3pHtBpnw/132"
                    //     },
                    //     "ResultCode": 0,
                    //     "ResultMsg": "授权成功"
                    // };
                    var data = {
                        "ResultData": {
                            "id": null,
                            "wxopenid": decodeURI(YKT.QueryString("openid")),
                            "truename": decodeURI(YKT.QueryString("NickName")),
                            "headurl": decodeURI(YKT.QueryString("HeadimgUrl"))
                        },
                        "ResultCode": 0,
                        "ResultMsg": "授权成功"
                    };
                    alert(JSON.stringify(data));
                    setUserInfo(data);
                    fn();
                } else {
                    location.href = 'http://www.yikutian.com/wx/Authorize.html';
                }
            }
        })
    }
}

/*通过名称获取 QueryString*/
YKT.QueryString = function (name) {
    var result = window.location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
    if (result == null || result.length < 1) {
        return "";
    }
    return result[1];
}

function wxFunction(fn) {
    var userInfo = getUserInfo();
    var wxopenid = userInfo.ResultData.wxopenid;
    ajax.request({
        params: {
            action: 'wxlocation'
        },
        callback: function (res) {
            ajax.request({
                params: {
                    action: 'wxsetting'
                },
                callback: function (app) {
                    wx.config({
                        debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                        appId: app.AppId, // 必填，公众号的唯一标识
                        timestamp: res.nonceStr, // 必填，生成签名的时间戳
                        nonceStr: res.timeStamp, // 必填，生成签名的随机串
                        signature: res.ticket, // 必填，签名
                        jsApiList: ['getLocation'] // 必填，需要使用的JS接口列表
                    });
                    fn();
                }
            })
        }
    })
}


// 分享成功调用接口
function changechallnum(activeId) {
    ajax.returnRequest({
        params: {
            action: 'changechallnum',
            wxopenid: getUserInfo().ResultData.wxopenid,
            activeid:activeId
        },
        callback: function (res) {
            console.log(res)
            if(res.ResultCode==0){
                mui.alert('分享成功', '提示', function() {
                    location.reload()
                });
            }else{
                mui.toast('分享失败');
            }
        }
    })
}